<template>
  <div class="app-container">
    <div class="search-box">
      <div class="search-hd">监控源管理</div>
      <div class="search-bd">
        <!-- 新增监控源的dialog对话框 Form -->
        <el-button type="success" @click="dialogFormVisible = true" size="mini"
          >新增</el-button
        >
        <el-dialog title="新增监控源" :visible.sync="dialogFormVisible">
          <el-form :model="form">
            <el-form-item label="监控项目" :label-width="formLabelWidth">
              <el-input
                v-model="form.mProject.name"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="监控源" :label-width="formLabelWidth">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="源类型" :label-width="formLabelWidth">
              <el-input v-model="form.type" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="告警配置" :label-width="formLabelWidth">
              <el-select v-model="form.configs" placeholder="请选择">
                <el-option label="日志提醒" value="1"></el-option>
                <el-option label="告警提醒" value="4"></el-option>
              </el-select>
              <!-- <el-input v-model="form.configs" autocomplete="off"></el-input> -->
            </el-form-item>
            <el-form-item label="提醒人" :label-width="formLabelWidth">
              <el-input
                v-model="form.contact"
                autocomplete="off"
                placeholder="请输入正确的邮箱地址"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addObject(form)">确 定</el-button>
          </div>
        </el-dialog>
        <!-- 修改监控源的dialog对话框 Form -->
        <el-dialog title="修改监控源" :visible.sync="dialogUpdateFormVisible">
          <el-form :model="form">
            <el-form-item label="监控项目" :label-width="formLabelWidth">
              <el-input
                v-model="form.mProject.name"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="监控源" :label-width="formLabelWidth">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="源类型" :label-width="formLabelWidth">
              <el-input v-model="form.type" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="告警配置" :label-width="formLabelWidth">
              <!--<el-input v-model="form.configs" autocomplete="off"></el-input>-->
              <template>
                <el-select v-model="form.configs" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </template>
            </el-form-item>
            <el-form-item label="提醒人" :label-width="formLabelWidth">
              <el-input v-model="form.contact" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogUpdateFormVisible = false"
              >取 消</el-button
            >
            <el-button type="primary" @click="updateObject(form)"
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </div>
    </div>
    <div class="main-box">
      <div class="main-hd">监控源管理</div>
      <el-table
        v-loading="loading"
        :data="list"
        stripe
        max-height="500"
        fit
        highlight-current-row
      >
        <el-table-column label="序号" width="95" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column
          label="监控项目"
          prop="mProject.name"
          align="center"
        ></el-table-column>
        <el-table-column
          label="监控源"
          prop="name"
          align="center"
        ></el-table-column>
        <el-table-column
          label="源类型"
          prop="type"
          align="center"
        ></el-table-column>
        <!--<el-table-column label="告警配置" align="center">
          <template slot-scope="scope">
            <el-link type="primary" :underline="false" @click="set(scope.row.id)">配置</el-link>
          </template>
        </el-table-column>-->
        <!--<el-table-column label="配置" prop="configs" align="center"></el-table-column>-->
        <el-table-column
          prop="configs"
          label="配置"
          show-overflow-tooltip
          align="center"
        >
          <template slot-scope="scope">
            <p v-if="scope.row.configs == '1'">日志提醒</p>
            <p v-else-if="scope.row.configs == '4'">告警提醒</p>
          </template>
        </el-table-column>
        <el-table-column
          label="提醒人"
          prop="contact"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <!-- <el-button type="success" size="mini" @click="edit(scope.row)">修改</el-button>-->
            <el-button
              type="success"
              @click="updateObjectById(scope.row.id)"
              size="mini"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="open(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="page-box">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="searchField.page"
          :page-sizes="[10, 15, 20, 25, 30, 50]"
          :page-size="searchField.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog title="告警配置" :visible.sync="dialogVisible" width="500px">
      <el-checkbox-group v-model="checkList">
        <el-checkbox label="deadly">致命错误</el-checkbox>
        <el-checkbox label="serious">严重错误</el-checkbox>
        <el-checkbox label="commonly">一般错误</el-checkbox>
        <el-checkbox label="general">普通错误</el-checkbox>
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <!-- <el-button type="primary" @click="saveSet">确 定</el-button>-->
        <el-button type="primary" @click="updatexxObject(form)"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  findAll,
  findAlladd,
  findAlldeleteById,
  findAllselectById,
  findAlupdate,
  projectadd,
} from "@/api/table";

export default {
  data() {
    return {
      list: null,
      loading: true,
      total: 10,
      searchField: {
        pname: "",
        oname: "",
        type: "",
        startTime: "",
        endTime: "",
        page: 1,
        limit: 15,
        date: "",
      },
      checkedId: "",
      checkList: [],
      dialogVisible: false,
      dialogFormVisible: false,
      dialogUpdateFormVisible: false,
      form: {
        id: "",
        pid: "",
        name: "",
        type: "",
        configs: "",
        contact: "",
        isDelete: 0,
        address: "",
        extention: "",
        serialVersionUID: 0,
        mProject: {
          id: "",
          name: "",
          server: "",
          db: "",
          mission: "",
          service: "",
          number: "",
          provider: "",
          tel: "",
          extention: "",
        },
      },
      formLabelWidth: "120px",
      options: [
        {
          value: "1",
          label: "日志提醒",
        },
        {
          value: "4",
          label: "告警提醒",
        },
      ],
      value: "",
    };
  },
  created() {
    this.initListData();
  },
  methods: {
    // 配置
    set(id) {
      this.checkedId = id;
      this.dialogVisible = true;
    },
    saveSet() {
      console.log(this.checkList, this.checkedId);
      this.dialogVisible = false;
    },
    // 编辑
    edit(row) {
      console.log(row);
    },
    // 删除
    del(id) {
      console.log(id);
      findAlldeleteById(id).then((res) => {
        this.initListData();
      });
    },
    initListData() {
      this.loading = true;
      findAll(this.searchField).then((res) => {
        this.list = res.data.rows;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.searchField.limit = val;
      this.pagingQuery();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      console.log(`当前页: ` + val);
      this.searchField.page = val;
      this.pagingQuery();
    },
    // 初始化dialog弹框的内容
    initDialogContent() {
      this.form = {
        id: "",
        pid: "",
        name: "",
        type: "",
        configs: "",
        contact: "",
        isDelete: 0,
        address: "",
        extention: "",
        serialVersionUID: 0,
        mProject: {
          id: "",
          name: "",
          server: "",
          db: "",
          mission: "",
          service: "",
          number: "",
          provider: "",
          tel: "",
          extention: "",
        },
      };
    },
    // 添加监控源的dialog弹框
    addObject(object) {
      this.initDialogContent();
      findAlladd(object).then((res) => {
        this.dialogFormVisible = false;
        this.initDialogContent();
        var flag = res.flag;
        // eslint-disable-next-line eqeqeq
        if (flag == true) {
          this.$message.success("添加数据成功");
          this.initListData();
        } else {
          this.$message.error("请输入项目管理中存在的名字");
          this.initDialogContent();
        }
      });
    },
    // 删除确认的提示框
    open(id) {
      this.$confirm("此操作将永久删除该监控源, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.del(id);
      });
    },
    // 根据id查询项目进行回显
    updateObjectById(id) {
      // 根据id查询数据并回显
      this.dialogUpdateFormVisible = true;
      findAllselectById(id).then((res) => {
        this.form = res.data;
      });
    },
    // 更新监控源
    updateObject(object) {
      findAlupdate(object)
        .then((res) => {
          this.dialogUpdateFormVisible = false;
          var flag = response.data.flag;
          // eslint-disable-next-line eqeqeq
          if (flag == true) {
            this.$message.success("更新数据成功");
            this.initListData();
          } else {
            this.$message.error("更新数据失败");
            this.initDialogContent();
          }
        })
        .finally(this.initDialogContent);
    },
    // 分页查询函数
    pagingQuery: function () {
      findAll(this.searchField).then((res) => {
        this.list = res.data.rows;
        this.total = res.data.total;
        this.loading = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.redColor {
  color: red;
}
.search-box {
  margin-bottom: 15px;
  .search-hd {
    border-left: 2px solid #00a73c;
    padding: 10px;
    margin-bottom: 10px;
  }
  .search-bd {
    .el-button--small {
      margin-top: 3px;
    }
  }
}
.main-box {
  .main-hd {
    height: 40px;
    line-height: 40px;
    background: #00a73c;
    color: #fff;
    padding-left: 10px;
  }
  .page-box {
    padding: 10px;
    text-align: center;
  }
}
</style>
